#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int left = 0, right = 0;
        if (fruits.size() <= 2)
        {
            return fruits.size();
        }
        unordered_map<int, int> hashi;
        int n = 0;
        while (right < fruits.size()) {
            hashi[fruits[right++]]++;
            while (hashi.size() > 2) {
                hashi[fruits[left]]--;
                if (hashi[fruits[left]] == 0)
                    hashi.erase(fruits[left]);
                left++;
            }
            if (hashi.size() <= 2)
                n = max(n, right - left);
        }
        return n;
    }
};